//
//  SWHistoryViewController.swift
//  ChildStory
//
//  Created by linhan on 16/7/14.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWHistoryViewController: SWBaseViewController,UICollectionViewDataSource,UICollectionViewDelegate,SWAPIComponent
{
    private let CellIdentifier:String = "SWWishCollectionCell"
    private var _collectionView:UICollectionView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("SWHistoryViewController:viewDidAppear", view.frame)
    }
    
    private var _layoutFirstTime:Bool = true
    override func viewWillLayoutSubviews() {
        print("SWHistoryViewController:layout", view.frame)
        if _layoutFirstTime
        {
            _layoutFirstTime = false
            
            let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.itemSize = CGSizeMake(view.width, view.height)
            //横向最小间隔
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
            layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
            layout.scrollDirection = .horizontal
            
            _collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
            _collectionView?.backgroundColor = UIColor(hex: 0xf5f5f5)
            _collectionView?.register(SWHistoryCollectionCell.self, forCellWithReuseIdentifier: CellIdentifier)
            _collectionView?.dataSource = self
            _collectionView?.delegate = self
            view.addSubview(_collectionView!)
            
            
            let backBtn:UIButton = SWAppearanceManager.sharedManager().createBackButton()
            backBtn.addTarget(self, action: #selector(backBtnTapped), for: .touchUpInside)
            backBtn.center = CGPointMake(20, 20)
            view.addSubview(backBtn)
            
            loadData()
        }
    }
    
    private func setup()
    {
        automaticallyAdjustsScrollViewInsets = false
        
    }
    
    private func loadData()
    {
        let req:SWHistoriesInfoReq = SWHistoriesInfoReq()
        req.successCallback = {[weak self] data,cache in
            print("data:", data)
            if let rsp = data as? SWHistoriesInfoRsp
            {
                self?._collectionView?.reloadData()
            }
        }
        load(req)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell:SWHistoryCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier:CellIdentifier, for: indexPath) as! SWHistoryCollectionCell
        cell.indexPath = indexPath
        return cell
    }
    
    @objc private func backBtnTapped()
    {
        navigationController?.popViewController(animated: true)
    }
    
}
